<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;

/**
 * 公告接口控制器
 */
class Announcement extends ApiBase
{
    
    /**
     * 获取公告列表
     */
    public function getAnnouncementList()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['page', 'limit', 'type', 'announcement_status']);
        return $this->apiReturn($this->logicAnnouncement->getAnnouncementList($filteredData));
    }
    
    /**
     * 获取公告详情
     */
    public function getAnnouncementInfo()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['id']);
        return $this->apiReturn($this->logicAnnouncement->getAnnouncementInfo($filteredData));
    }
    
    /**
     * 获取最新公告
     */
    public function getLatestAnnouncement()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['limit']);
        return $this->apiReturn($this->logicAnnouncement->getLatestAnnouncement($filteredData));
    }
    
    /**
     * 标记公告为已读
     */
    public function markAsRead()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['id', 'user_token']);
        return $this->apiReturn($this->logicAnnouncement->markAnnouncementAsRead($filteredData));
    }
    
    /**
     * 批量标记公告为已读
     */
    public function markMultipleAsRead()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['ids', 'user_token']);
        return $this->apiReturn($this->logicAnnouncement->markMultipleAnnouncementsAsRead($filteredData));
    }
    
    /**
     * 获取用户未读公告数量
     */
    public function getUnreadCount()
    {
        $data = $this->request->param();
        $filteredData = $this->filterApiParams($data, ['user_token']);
        return $this->apiReturn($this->logicAnnouncement->getUnreadAnnouncementCount($filteredData));
    }
    

    
    /**
     * 过滤API参数，只保留指定的业务字段
     */
    private function filterApiParams($data, $allowedFields)
    {
        $filtered = [];
        foreach ($allowedFields as $field) {
            if (isset($data[$field])) {
                $filtered[$field] = $data[$field];
            }
        }
        return $filtered;
    }
}